package chase.java.show;

import chase.java.algorithm.AStar;
import chase.java.unit.Edge;
import chase.java.unit.Point;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

public class Draw {
    static final int with = 650;
    static final int length = 600;

    static double initLarge = 1.07;
    //25000
    static double enlarge = 25000;
    Point start = null;
    Point goal = null;



    double pointX = 0;
    double pointY = 0;


    double xMin = Double.MAX_VALUE;
    double yMin = Double.MAX_VALUE;

    int turn = -1;


    JFrame frame = new JFrame();
    List<Edge> edges;
    Edge path;
    Canvas drawArea;

    class DrawCanvas extends Canvas {

        @Override
        public void paint(Graphics g) {

                g.setColor(Color.BLACK);
                for (Edge edge : edges) {
                    for (int i = 0; i < edge.length() - 1; ++i) {
                        g.drawLine((int) (edge.get(i).getX()), (int) (edge.get(i).getY()),
                                (int) (edge.get(i + 1).getX()), (int) (edge.get(i + 1).getY()));
                    }
                }

                g.setColor(Color.RED);
                if (path != null)
                    for (int i = 0; i < path.length() - 1; ++i) {
                        g.drawLine((int) (path.get(i).getX()), (int) (path.get(i).getY()),
                                (int) (path.get(i + 1).getX()), (int) (path.get(i + 1).getY()));
                    }

                g.setColor(Color.GREEN);
                if (start != null) {
                    g.fillOval((int) start.getX() - 4, (int) start.getY() - 4, 8, 8);
                }

                g.setColor(Color.RED);
                if (goal != null) {
                    g.fillOval((int) goal.getX() - 4, (int) goal.getY() - 4, 8, 8);
                }

        }
    }

    void zoom(){
        for (Edge edge : edges) {
            for (int i = 0; i < edge.length(); ++i) {
                edge.get(i).setX(edge.get(i).getX() * enlarge+pointX);
                edge.get(i).setY(edge.get(i).getY() * enlarge+pointY);
            }
        }

        if(path != null)
        for (int i = 0; i < path.length(); ++i) {
            path.get(i).setX(path.get(i).getX() * enlarge+pointX);
            path.get(i).setY(path.get(i).getY() * enlarge+pointY);
        }
        if(start != null) {
            start.setX(start.getX() * enlarge + pointX);
            start.setY(start.getY() * enlarge + pointY);
        }
        if(goal != null) {
            goal.setX(goal.getX() * enlarge + pointX);
            goal.setY(goal.getY() * enlarge + pointY);
        }
    }


    void converter() {

        zoom();

        for (Edge edge : edges) {
            for (int i = 0; i < edge.length(); ++i) {
                if (edge.get(i).getX() < xMin)
                    xMin = edge.get(i).getX();
                if (edge.get(i).getY() < yMin)
                    yMin = edge.get(i).getY();
            }
        }
        for (Edge edge : edges) {
            for (int i = 0; i < edge.length(); ++i) {
                edge.get(i).setX(edge.get(i).getX() - xMin + 10);
                edge.get(i).setY(edge.get(i).getY() - yMin + 10);
            }
        }
    }


    public void draw(List<Edge> edges) {
        this.edges = edges;

        converter();

        drawArea = new DrawCanvas();
        drawArea.setSize(with, length);

        //添加滚动缩放事件
        drawArea.addMouseWheelListener(e -> {
            int rotation = e.getWheelRotation();
            if (rotation == 1)
                enlarge = 1 / initLarge;
            else
                enlarge = initLarge;

            pointX = e.getX()*(1-enlarge);
            pointY = e.getY()*(1-enlarge);

            zoom();
            drawArea.repaint();
        });


        //设置添加起点终点事件
        drawArea.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == 1) {
                    Point local = new Point(e.getX(),e.getY());
                    if (turn == -1)
                        start = local;
                    else
                        goal = local;
                    drawArea.repaint();
                    path = null;
                }
            }
        });

        JButton btn = new JButton("生成路径");
        btn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                if (e.getButton() == 1) {
                    path=AStar.execute(start,goal,edges);
                    drawArea.repaint();
                }
            }
        });


        JButton startBtn = new JButton("设置起点");
        JButton goalBtn = new JButton("设置终点");


        MouseAdapter turn = new MouseAdapter(){
            @Override
            public void mouseReleased(MouseEvent e) {
                if (e.getButton() == 1) {
                    if(e.getComponent().equals(startBtn) )
                        Draw.this.turn = -1;
                    else if(e.getComponent().equals(goalBtn))
                        Draw.this.turn = 1;
                }
            }
        };

        startBtn.addMouseListener(turn);
        goalBtn.addMouseListener(turn);


        Box box = Box.createHorizontalBox();

        box.add(startBtn);
        box.add(goalBtn);
        box.add(btn);

        frame.add(drawArea);


        frame.add(box, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

}
